<?php
namespace app\home\controller;
use think\Db;
use think\Request;
class Log
{
    public function __construct()
    {
        set_cors();
    }
    protected $table_user_game_log= 'user_game_log';              /* 游戏用户进入游戏记录 */
    protected function Db_connect_user_game_log(){
        $props= Db::connect('db_config3')->table($this->table_user_game_log);
        return $props;
    }
    function error_log(Request $request)
    {
        set_cors();
        $param = $request->param();
        if(empty($param['key']) || empty($param['msg']) || empty($param['version'])){
            puterror(1);
        }
        $time =date('Y-m-d H:i:s',time());
        $current_day = date('Y-m-d');
        file_put_contents('client_log/'.$param['version'].'_'.$current_day.'.txt','['.$time.']'.' 错误标识:' . $param['key'] . " 错误原因:".$param['msg'].PHP_EOL.PHP_EOL, FILE_APPEND);
        $data_Arr['code']='success';
        echo json_encode($data_Arr);exit;
    }

    // 游戏log
    function info_log(Request $request)
    {
        $param = $request->param();
        if(empty($param['msg'])){
            puterror(1);
        }
        $time =date('Y-m-d H:i:s',time());
        $day=date('Y-m-d',time());

        file_put_contents('info_game/'.$day.'.txt', '['.$time.']'.' '.$param['msg'].PHP_EOL,FILE_APPEND);


        $data_Arr['code']='success';
        echo json_encode($data_Arr);exit;
    }
    function user_game_log(Request $request)
    {
        $param = $request->param();
        if(empty($param['UserID']) || empty($param['starttime'])){
            puterror(1);
        }
        $data = [
            'UserID' => $param['UserID'],
            'starttime' => $param['starttime']
        ];
        $res =$this->Db_connect_user_game_log()->insert($data);
        if($res){
            $data_Arr['code']='success';
            echo json_encode($data_Arr);exit;
        }
    }
}
